// 云函数入口文件
//  来源掘金文章《猫咪卡通变 - 小程序》教程：https://juejin.cn/post/7028624704491487269
const cloud = require('wx-server-sdk')
var rp = require('request-promise')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  let datas = event.datas
  datas.access_token = '24.2fcb174428ef04330e034a5f8f17d513.2592000.1639067518.282335-25137587' // 这里access_token需要你去百度云请求获取(官方地址：https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu)
  return new Promise((resolve, reject) => {
    rp({
      url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal',
      method: "POST",
      json: true,
      form: event.datas,
    }, function (error, response, body) {
      console.log("响应" + body)
      resolve(body)
      if (!error && response.statusCode == 200) {
        try {} catch (e) {
          reject()
        }
      }
    })
  })
}